<?php

namespace app\api\controller;

use app\admin\model\ClassAddress;
use app\admin\model\Club;
use app\admin\model\ClubDtl;
use app\admin\model\SearchShool;
use app\admin\model\TeacherArea;
use app\admin\model\TeacherCourseDisplay;
use app\common\controller\Api;

/**
 * 校方接口
 */
class School extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    //校方地址
    public function address(){
        $params = $this->request->param();
        if(!isset($params['page'])||!$params['page']){
            $params['page'] = 1;
        }
        if(!isset($params['page_size'])||!$params['page_size']){
            $params['page_size'] = 10;
        }
        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }

        $address = ClassAddress::where(['user_id'=>$user['id']])->order('sort','desc')->order('id','desc')
            ->paginate($params['page_size']);

        $list = $address->getCollection();
        foreach ($list as $k=>$v){
            $area_arr = TeacherArea::get_arr();
            $list[$k]['area_name'] = isset($area_arr[$v['area']]) ? $area_arr[$v['area']] : '';
        }
        $count = $address->total();
        $data = [];
        $data['list'] = $list;
        $data['count'] = $count;
        $this->success('校方地址',$data);
    }

    //添加地址
    public function add_address(){
        $params = $this->request->param();
        if(!isset($params['school'])||!$params['school']){
            $this->error('学校未填写');
        }
        if(!isset($params['detail'])||!$params['detail']){
            $this->error('详细地址未填写');
        }
        if(!isset($params['area'])||!$params['area']){
            $this->error('区域未选择');
        }
        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }
        $find = \app\admin\model\IdentityInfo::where(['user_id'=>$user['id']])->find();
        if(!$find){
            $this->error('普通用户无权限');
        }
        $add = new ClassAddress();
        $add->user_id   = $user['id'];
        $add->school    = $params['school'];
        $add->detail    = $params['detail'];
        $add->sort      = 0;
        $add->opt_for   = 2;
        $add->addtime   = date('Y-m-d H:i:s');
        $add->status    = 1;
        $add->area    = $params['area'];
        $add->save();
        $school = SearchShool::where(['name'=>$params['school'],'area'=>$params['area']])->find();
        if(!$school){
            $search_shool = new SearchShool();
            $search_shool->name = $params['school'];
            $area_name = TeacherArea::where(['id'=>$params['area']])->find();
            if(isset($area_name['name'])&&$area_name['name']){
                $search_shool->area = $area_name['name'];
            }else{
                $search_shool->area = $params['area'];
            }
            $search_shool->addtime = date('Y-m-d H:i:s');
            $search_shool->save();
        }
        $this->success('添加完成',$add);
    }

    //修改地址
    public function up_address(){
        $params = $this->request->param();
        if(!isset($params['id'])||!$params['id']){
            $this->error('id必传');
        }
        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }

        $update = [];
        if(isset($params['school'])&&$params['school']){
            $update['school'] = $params['school'];
        }
        if(isset($params['detail'])&&$params['detail']){
            $update['detail'] = $params['detail'];
        }
        if(isset($params['area'])&&$params['area']){
            $update['area'] = $params['area'];
        }
        $count = count($update);
        if(!$count){
            $this->error('无改动');
        }
        ClassAddress::where(['id'=>$params['id'],'user_id'=>$user['id']])->update($update);
        $find = ClassAddress::where(['id'=>$params['id'],'user_id'=>$user['id']])->find();
        if($find){
            $school = SearchShool::where(['name'=>$find['school'],'area'=>$find['area']])->find();
            if(!$school){
                $search_shool = new SearchShool();
                $search_shool->name = $find['school'];
                $area_name = TeacherArea::where(['id'=>$params['area']])->find();
                if(isset($area_name['name'])&&$area_name['name']){
                    $search_shool->area = $area_name['name'];
                }else{
                    $search_shool->area = $params['area'];
                }
                $search_shool->addtime = date('Y-m-d H:i:s');
                $search_shool->save();
            }
        }
        $this->success('修改完成',$update);
    }

    //选中地址
    public function opt_for_address(){
        $params = $this->request->param();
        if(!isset($params['id'])||!$params['id']){
            $this->error('id必传');
        }
        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }

        ClassAddress::where(['user_id'=>$user['id']])->update(['opt_for'=>2]);
        ClassAddress::where(['id'=>$params['id'],'user_id'=>$user['id']])->update(['opt_for'=>1]);
        $this->success('已选中',[]);
    }

    public function area_pull(){
        $area_arr = TeacherArea::get_arr();
        $area_id_arr = TeacherArea::get_id_arr();
        $data['area'] = $area_arr;
        $data['area_arr'] = $area_id_arr;
        $this->success('地址下拉',$data);
    }
}
